﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using MySql.Data.MySqlClient;
using DatabaseHandler;

namespace Trinity_Database_Handler
{
    public partial class Form1 : Form
    {
        ConnectionHandler ConnectionHandlerLib = new ConnectionHandler();
        public Form1()
        {
            InitializeComponent();
            username.Focus();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            //Open http://forum.trinitycore.org with the webbrowser
            System.Diagnostics.ProcessStartInfo
            procFormsBuilderStartInfo = new System.Diagnostics.ProcessStartInfo();
            procFormsBuilderStartInfo.FileName = "http://forum.trinitycore.org/";
            procFormsBuilderStartInfo.WindowStyle =  System.Diagnostics.ProcessWindowStyle.Maximized;
            System.Diagnostics.Process procFormsBuilder = new System.Diagnostics.Process();
            procFormsBuilder.StartInfo = procFormsBuilderStartInfo;
            procFormsBuilder.Start();

        }

        public void Login_Click(object sender, EventArgs e)
        {
            // Assign the textbox inputs to strings
            string Hostname = hostname.Text;
            string Username = username.Text;
            string Password = password.Text;
            string Worlddb = worlddb.Text;

            // Check if there is an empty field
            if (Username == "" || Password == "" || Worlddb == "" ||Hostname == ""){
                MessageBox.Show("One or more fields are empty.");
            }
            else{
                //if the checkbox is checked
                if (checkBox1.Checked)
                {

                    // Delete the file with the old info if there is one
                    if (System.IO.File.Exists("serverinfo.txt"))
                    {
                        System.IO.File.Delete("serverinfo.txt");
                    }

                    // create a textwriter and open the file
                    TextWriter tw = new StreamWriter("serverinfo.txt");

                    //write the info
                    tw.WriteLine(Hostname);
                    tw.WriteLine(Username);
                    tw.WriteLine(Password);
                    tw.WriteLine(Worlddb);

                    // Close the stream
                    tw.Close();
                }
                    MySqlConnection Connection = ConnectionHandlerLib.BuildConnection(Hostname, Worlddb, Username, Password);

                    //Test the connection, if the connection is succeed go to Form2
                    if (ConnectionHandlerLib.OpenConnection(Connection))
                    {
                        this.Hide();
                        Form f2 = new Form2(Username, Password, Hostname, Worlddb);
                        f2.Show();
                    }
                
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // check if there is a serverinfo.txt file available
            if (System.IO.File.Exists("serverinfo.txt")){
                StreamReader sr = new StreamReader("serverinfo.txt");
                string Hostname = sr.ReadLine();
                string Username = sr.ReadLine();
                string Password = sr.ReadLine();
                string Worlddb = sr.ReadLine();
                sr.Close();
                hostname.Text = Hostname;
                username.Text = Username;
                password.Text = Password;
                worlddb.Text = Worlddb;
               
            }
        }

    }
}
